﻿using System.ComponentModel.Design.Serialization;
using System.Reflection.Metadata.Ecma335;

namespace _1
{
    internal class Program
    {
        abstract public class Shape
        {
            public String type;
            public int edge1;
            public int edge2;
            public int edge3;
            public double area;
            public abstract  double Area();
            public abstract bool Judge();
            public Shape(String type,int edg1) { this.type = type;this.edge1 = edg1; }
            public Shape(String type, int edg1,int edg2) { this.type = type; this.edge1 = edg1; this.edge2 = edg2; }
            public Shape(String type, int edg1,int edg2,int edg3) { this.type = type; this.edge1 = edg1; this.edge2 = edg2; this.edge3 = edg3;  }
        }
        class Rectangle : Shape
        {
            public Rectangle(String type,int a,int b) : base(type,a,b) { }
            public override double Area() 
            {
                area = edge1 * edge2;
                Console.WriteLine("The area is :" + area);
                return area; 
            }
            public override bool Judge()
            {
                if (edge1 <= 0 || edge2 <= 0||edge1 ==edge2 )
                {
                    Console.WriteLine("The objection is illegal");
                    return false;
                }
                else { Console.WriteLine("The objection is legal"); return true; }
            }
        }
        class Square : Shape
        {



            public Square(String type,int a) : base(type,a) { }
            public override double Area() 
            {
                area = edge1 * edge1;
                Console.WriteLine("The area is :" + area);
                return area; 
            }
            public override bool Judge()
            {
                if (edge1 <= 0 )
                {
                    Console.WriteLine("The objection is illegal");
                    return false;
                }
                else { Console.WriteLine("The objection is legal"); return true; }
            }
        }
        class Triangle : Shape
        {
            public Triangle(String type,int a,int b,int c) : base(type,a,b,c) { }
            public override double Area() 
            {
                int p = (edge1 + edge2 + edge3) / 2;
                area= Math.Sqrt(p * (p - edge1) * (p - edge2) * (p - edge3));
                Console.WriteLine("The area is :" + area);
                return area; 
            }
            public override bool Judge()
            {
                if (edge1+edge2 <= edge3|| edge1 + edge3 <= edge2|| edge3 + edge2 <= edge1)
                {
                    Console.WriteLine("The objection is illegal");
                    return false;
                }
                else { Console.WriteLine("The objection is legal"); return true; }
            }
        }
        public class Factory
        {
            public Shape CreateShape(int a)
            {
                Random Rd1 = new Random();
                Random Rd2 = new Random();
                Random Rd3 = new Random();
                int rd1 = Rd1.Next(1, 100);
                int rd2 = Rd2.Next(1, 100);
                int rd3 = Rd3.Next(1, 100);
                switch (a)
                {
                    case 1: return new Rectangle("长方形",rd1,rd2);
                    case 2: return new Square("正方形",rd1);
                    case 3: return new Triangle("三角形",rd1,rd2,rd3);
                    default: break;
                }
                return null;
            }
        }
        static void Main()
        {
            double area_sum = 0;
            for (int i = 0; i < 10; i++)
            {
                Random rd = new Random();
                int a = rd.Next(1, 4);
                Factory obj = new Factory();
                Shape  shape = obj.CreateShape(a);
                Console.WriteLine(shape.type);
                bool res=shape.Judge();
                if (res)
                {
                    if (shape.type == "正方形")
                    {
                        Console.WriteLine("The edge is:" + shape.edge1);
                    }
                    else if (shape.type == "长方形")
                    {
                        Console.WriteLine("The two edge is :" + shape.edge1 + "," + shape.edge2);
                    }
                    else 
                    {
                        Console.WriteLine("The three edge is :" + shape.edge1 + "," + shape.edge2+","+shape .edge3);
                    }
                        area_sum += shape.Area();
                }
                Console.WriteLine(" ");
            }
            Console.WriteLine("The sum of the area is:" + area_sum);
        }
    }
}
